﻿Imports TNCDAL
Imports DevComponents.DotNetBar
Public Class ucSellingRateOneFile
    Private _idFile As Integer
    Private _ctrlHBL As New CHBLController
    Dim listUc As ArrayList = Nothing
    Dim hbls As IQueryable(Of tblHBL)
    Dim curUc As ucSellingHBL = Nothing
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Public Sub New(ByVal idFile As Integer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _idFile = idFile
        hbls = _ctrlHBL.SelectAllRecordByFile(_idFile)
    End Sub

    Private Sub ucSellingRateOneFile_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        EnableControls(False)
        LoadThongTin()
        If (listUc.Count > 0) Then
            curUc = listUc.Item(0)
        End If
    End Sub
    Private Sub LoadThongTin()
        If (hbls IsNot Nothing) Then
            If (hbls.Count > 0) Then
                If (listUc Is Nothing) Then
                    NewListUCSellingHBL()
                Else
                    'chỗ này dùng để kiểm tra trường hợp sau
                    'lỡ người ta thay đổi số lượng hbl thì sao
                    'lúc đó phải new lại hết array list
                    'Do đó, phải kiểm tra xem số lượng hbls và listUC có giống nhau
                    'nếu giống nhau thì load lại grid thôi
                    'bằng ko thì new lại hết listUC
                    'cách xử lý như vậy cũng vẫn bị lủng ở chỗ là nếu người ta xoá cái này, thêm cái khác thì số lượng ko thay đổi nhưng ID_HBL thay đổi rồi
                    'nhưng thường trường hợp này ít (????)
                    If (listUc.Count <> hbls.Count) Then
                        NewListUCSellingHBL()
                    Else
                        For Each uc As ucSellingHBL In listUc
                            uc.BindGridSelling()
                        Next
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub NewListUCSellingHBL()
        listUc = New ArrayList()
        'hiển thị mỗi HBL 1 tab
        For Each h In hbls
            Dim uc As New ucSellingHBL(h.ID_HBL)
            Dim sName As String = "Selling HBL: " & h.HBLNo
            listUc.Add(uc)
            AddTab(uc, sName)
        Next
    End Sub
    Private Sub AddTab(ByVal uc As Windows.Forms.UserControl, ByVal sName As String)
        Dim tab As SuperTabItem = tabHBLs.CreateTab(sName)
        tab.AttachedControl.Controls.Add(uc)
        uc.Dock = DockStyle.Fill
    End Sub
    'Private Sub BindGridBuying()
    '    Dim buyings = ctrlSelling.GetBuyingRateListToSuggest(_idFile)
    '    gridBuying.DataSource = buyings
    'End Sub
    Private Sub EnableControls(ByVal val As Boolean)
        'gridSelling.ReadOnly = Not val
        btncancel.Enabled = val
        btnSave.Enabled = val
        btnEdit.Enabled = Not val
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'lấy ra user control hiện thời
        If (curUc IsNot Nothing) Then
            curUc.EditSellingRate()
            EnableControls(True)
        End If
    End Sub

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        'lấy ra user control hiện thời
        If (curUc IsNot Nothing) Then
            curUc.CancelSellingRate()
            EnableControls(False)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (curUc IsNot Nothing) Then
            curUc.SaveSellingRate()
            EnableControls(False)
        End If

    End Sub

    Private Sub btPrintDebit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPrintDebit.Click
        'Thống kê tổng giá mua bán
        Dim curIDHBL As Integer = -1
        If (curUc IsNot Nothing) Then
            curIDHBL = curUc.ID_HBL
        End If
        frmMain.AddNewDockContainerItem("DebitNote", "Debit Note", New ucDebit(_idFile, curIDHBL))
    End Sub
    Private Sub tabHBLs_SelectedTabChanged(ByVal sender As System.Object, ByVal e As DevComponents.DotNetBar.SuperTabStripSelectedTabChangedEventArgs) Handles tabHBLs.SelectedTabChanged
        If (listUc.Count > 0) Then
            curUc = listUc.Item(tabHBLs.SelectedTabIndex)
        End If
    End Sub

    Private Sub btReCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btReCompute.Click
        EnableControls(False)
        If (curUc IsNot Nothing) Then
            curUc.ReCompute()
        End If
    End Sub

    'Sỹ code
    Private Sub btnReceiptVoucher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceiptVoucher.Click
        If (listUc.Count > 0) Then
            curUc = listUc.Item(tabHBLs.SelectedTabIndex)
            'Dim m_av As New MAdvanceVoucher
            'Dim obj_av As tblAdvanceVoucher
            'obj_av = m_av.SelectRecordById(1)

            Dim uc As New ucReceiptVouchersNew(curUc.ID_HBL)
            frmMain.AddNewDockContainerItem("Receipt Vouchers", "Receipt Vouchers", uc)
            'AddNewDockCategoriesItem()

        End If
    End Sub
    Public Function CoThongTin() As Boolean
        If (hbls Is Nothing) Then
            Return False
        End If
        If (hbls.Count > 0) Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub RefreshData()
        hbls = _ctrlHBL.SelectAllRecordByFile(_idFile)
        LoadThongTin()
    End Sub
End Class
